package Day13;

import com.sun.org.apache.xpath.internal.WhitespaceStrippingElementMatcher;

import java.io.*;

/**
 * @Author kalista
 * @Description 缓冲字节流
 * @Date 2020/11/19  15:26
 **/
public class TestBuffederFileCopye {
    public static void main(String[] args) throws IOException {

        // 开始时间
        long startTime = System.currentTimeMillis();// 开始时间戳
        copyFileNoBuffer("f:\\duotai.mp4","f:\\duotai1.mp4");
        long endTime = System.currentTimeMillis();// 结束时间戳
        System.out.println("没有缓冲流的时间是" + (endTime-startTime));

        System.out.println("============== 下面是缓冲流的 ==============");
        long startTimeBuffer = System.currentTimeMillis();// 开始时间戳
        copyFileBuffer("f:\\duotai.mp4","f:\\duotai2.mp4");
        long endTimeBuffer = System.currentTimeMillis();// 结束时间戳
        System.out.println("使用缓冲流的时间：" + (endTimeBuffer - startTimeBuffer));
    }


    /**
     * 没有缓冲流
     *
     * @param str 源文件
     * @param dec 目标文件
     */
    public static void copyFileNoBuffer(String str, String dec) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(str);
        FileOutputStream fileOutputStream = new FileOutputStream(dec);
        int temp = 0;
        while ((temp = fileInputStream.read()) != -1) {
            fileOutputStream.write(temp);
        }
        fileOutputStream.close();
        fileInputStream.close();
    }

    /**
     * 带缓冲流
     *
     * @param str
     * @param dec
     */
    public static void copyFileBuffer(String str, String dec) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(str);
        FileOutputStream fileOutputStream = new FileOutputStream(dec);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        int temp = 0;
        while ((temp=bufferedInputStream.read()) != -1){
            bufferedOutputStream.write(temp);
        }
        bufferedOutputStream.close();
        bufferedInputStream.close();
        fileOutputStream.close();
        fileInputStream.close();
    }


}
